<template>
  <div>
    <navComp :title="$route.meta.title" :back="$route.meta.back"></navComp>
    <div class="box">
      <van-contact-card
        v-if="buffer"
        type="edit"
        :name="address.username"
        :tel="address.userphone"
        @click="onEdit"
      />
      <van-contact-card
        v-else
        type="add"
        add-text="添加收货地址"
        @click="onAdd"
      />
      <van-card
        v-for="item in orderData"
        :key="item.id"
        :num="item.num"
        :price="item.price"
        desc="描述信息"
        :title="item.goodsname"
        :thumb="$pre + item.img"
      />
      <van-submit-bar
        :price="Number(countMoney) * 100"
        button-text="提交订单"
        @submit="onSubmit"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  data() {
    return {
      uid: this.$utils.getUid(),
      orderData: [],
      countMoney: "",
      countNumber: "",
      buffer: true,
      address: {},
    };
  },
  created() {},
  mounted() {
    let { submit, countMoney, countNumber } = JSON.parse(
      localStorage.getItem("orderData")
    );
    this.orderData = submit;
    this.countMoney = countMoney;
    this.countNumber = countNumber;
    let address = this.$route.query.address || "";
    if (address != "") {
      let addressDeta = JSON.parse(address);
      this.address = {
        id: addressDeta.id,
        username: addressDeta.name,
        userphone: addressDeta.tel,
      };
    } else {
      this.getUserAddress();
    }
  },
  methods: {
    onEdit() {},
    onAdd() {
      this.$router.push("/address");
    },
    getUserAddress() {
      this.$api
        .reqGetAddresslist({ uid: this.uid, status: 1 })
        .then((res) => {
          console.log(res);
          if (res == null) {
            this.buffer = false;
          } else {
            this.buffer = true;
            this.address = res.data.list;
          }
        })
        .catch((err) => console.log(err));
    },
    onSubmit() {
      // 1.检测收获地址
      if (Object.keys(this.address).length == 0) {
        this.$notify({
          type: "warning",
          message: "完善收获地址",
        });
        return;
      }
      let inserData = {
        uid: this.uid,
        countmoney: this.countMoney,
        countnumber: this.countNumber,
        addressid: this.address.id,
      };
    },
    onEdit() {
      this.$router.push("/list?addressId=" + this.address.id);
    },
  },
};
</script>
<style scoped lang="scss">
.box {
  margin-top: 40px;
}
</style>
